package com.zhangyujun.bysj.manager.controller;


import com.zhangyujun.bysj.manager.service.SysMenuService;
import com.zhangyujun.bysj.manager.service.ValidateCodeService;
import com.zhangyujun.bysj.model.dto.system.LoginDto;
import com.zhangyujun.bysj.model.vo.common.Result;
import com.zhangyujun.bysj.model.vo.common.ResultCodeEnum;
import com.zhangyujun.bysj.model.vo.system.LoginVo;
import com.zhangyujun.bysj.manager.service.SysUserService;
import com.zhangyujun.bysj.model.vo.system.SysMenuVo;
import com.zhangyujun.bysj.model.vo.system.ValidateCodeVo;
import com.zhangyujun.bysj.utils.AuthContextUtil;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.v3.oas.annotations.Operation;

import java.util.List;


@Tag(name = "用户接口")
@RestController
@RequestMapping(value = "/admin/system/index")
public class IndexController {

    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private ValidateCodeService validateCodeService;
    @Autowired
    private SysMenuService sysMenuService;
    //用户退出
    @Operation(summary = "用户退出的方法")
    @GetMapping("/logout")
    public Result logout(@RequestHeader(value = "token") String token) {
        sysUserService.logout(token);
        return Result.build(null, ResultCodeEnum.SUCCESS);

    }
    //获取当前登录用户信息
    @Operation(summary = "获取当前登录用户信息的方法")
    @GetMapping("/getUserInfo")
    public Result getUserInfo() {
        return Result.build(AuthContextUtil.get(), ResultCodeEnum.SUCCESS);
    }

//    @Operation(summary = "获取当前登录用户信息的方法")
//    @GetMapping("/getUserInfo")
//    public Result getUserInfo(@RequestHeader(value = "token") String token) {
//        //1 从请求头获取token
//        //2 根据token查询redis获取用户信息
//        SysUser sysUser = sysUserService.getUserInfo(token);
//        //3 用户信息返回
//        return Result.build(sysUser, ResultCodeEnum.SUCCESS);
//    }

    //生成图片验证码
    @Operation(summary = "生成图片验证码的方法")
    @GetMapping("/generateValidateCode")
    public Result<ValidateCodeVo> generateValidateCode() {
        ValidateCodeVo validateCodeVo = validateCodeService.generateValidateCode();
        return Result.build(validateCodeVo, ResultCodeEnum.SUCCESS);
    }

    //用户登录
    @Operation(summary = "登录的方法")
    @PostMapping("login")
    public Result login(@RequestBody LoginDto loginDto) {
        LoginVo loginVo = sysUserService.login(loginDto);
        return Result.build(loginVo, ResultCodeEnum.SUCCESS);
    }
    //查询用户可以操作的菜单
    @GetMapping("/menus")
    public Result menus() {
        List<SysMenuVo> sysMenuVoList =  sysMenuService.findUserMenuList() ;
        return Result.build(sysMenuVoList , ResultCodeEnum.SUCCESS) ;
    }

}
